var agentIp = [
    'http://agent.szaltair.com:8080/proxyservice/TransferServlet',
    'http://119.29.54.250:8090/proxyservice/TransferServlet',
    'http://172.16.20.201:8080/proxyservice/TransferServlet',
    'http://192.168.10.5:8080/proxyservice/TransferServlet',
    'http://172.42.1.11:8080/proxyservice/TransferServlet',
    'http://172.16.30.214:8080/proxyservice/TransferServlet',
    'http://172.16.50.210:8080/proxyservice/TransferServlet',
    'http://172.16.40.210:8080/proxyservice/TransferServlet'
];
var mCloudIP = 'http://123.207.110.98:8080/wifi/TransferServlet';
var mCallback; //回调
var isCallbackState = true; //是否回调出去
var ismCloudIpNetWorkState = false; //云ip的网络情况
var isAgentMetWorkState = false; //代理的网络情况
var mAgentFwq; //代理服务器的地址
var mRetData;
var mSaveComboInt = 0; //记录模式的请求次数
var seekAgent = {
    //开始执行找代理
    startSeekAgent: function(callback) {
        isCallbackState = true;
        mAgentFwq ='';
        mRetData = '';
        mCallback = callback;
        seekAgent.judgeCloudNetwork();
        seekAgent.loopAgenIp();  
        seekAgent.delayedInspect();
        seekAgent.getWebpage();
    },

    //循环所有的代理
    loopAgenIp: function() {
        for (var i = 0; i < agentIp.length; i++) {
            seekAgent.getAgenIp(agentIp[i]);
        }
    },

    //具体执行 判断代理地址是否通
    getAgenIp: function(requesturl) {
      var parame = {
        userid:'0',
        visitType:1,
        ip:$api.getStorage(NewFiData.wifiKey.wifiIp),
        mac:$api.getStorage(NewFiData.wifiKey.phoneMac),
        apMac:$api.getStorage(NewFiData.wifiKey.apMac),
        apName:$api.getStorage(NewFiData.wifiKey.wifiName)
      }
      var params = common.getJSONParame('version_isAlive',JSON.stringify(parame));
        api.ajax({
            url: requesturl,
            method: 'post',
            data: {
                values: params
            },
            timeout: 2
        }, function(ret, err) {
            if (ret.code == 1) {
                isAgentMetWorkState = true;
                if(ret.siteAreaid != 0){
                  var ip = $api.getStorage(NewFiData.wifiKey.wifiIp);
                  var apMac = $api.getStorage(NewFiData.wifiKey.apMac);
                  var phoneMac = $api.getStorage(NewFiData.wifiKey.phoneMac);
                  if(seekAgent.judgeWifiParam(ip,apMac,phoneMac)){
                    if (isCallbackState) {
                        isCallbackState = false;
                        mCallback(requesturl,ret);
                        api.closeFrame({
                            name: 'baidu'
                        });
                    }
                  }else {
                    mAgentFwq = requesturl;
                    mRetData = ret;
                  }
                }else {
                 mAgentFwq = requesturl;
                 mRetData = ret;
                }
            }else if(ret.code == 0 || ret.code == -1){
              mAgentFwq = requesturl;
              mRetData = ret;
              isAgentMetWorkState = true;
            }
        });
    },

      //判断 云地址 是否能通
      judgeCloudNetwork: function() {
        api.ajax({
            url: mCloudIP,
            method: 'post',
            data: {},
            timeout: 2
        }, function(ret, err) {
            if (ret) {
                if (ret.code <3 && ret.code >-3) {
                  ismCloudIpNetWorkState= true;
                  $api.setStorage(NewFiData.appParameKey.networkstate,true);
                }else {
                  ismCloudIpNetWorkState = false;
                  $api.setStorage(NewFiData.appParameKey.networkstate,false);
                }
            } else {
               ismCloudIpNetWorkState = false;
                 $api.setStorage(NewFiData.appParameKey.networkstate,false);
            }
        });

    },

    //判断云地址的联网状态(给外界调用)
    judgeCloudNetworkState:function(callback){
      api.ajax({
          url: mCloudIP,
          method: 'post',
          data: {},
          timeout: 2
      }, function(ret,err) {
          if (ret) {
              if (ret.code <3 && ret.code >-3) {
                callback(true);
              }else {
                callback(false);
              }
          } else {
              callback(false);
             ismCloudIpNetWorkState = false;
          }
      });
    },

    //延时去检查
    delayedInspect: function() {
        setTimeout(function() {
          //alert(isCallbackState+'...'+isAgentMetWorkState+'...'ismCloudIpNetWorkState);
          console.log('........时间到了....isCallbackState..'+isCallbackState+'...isAgentMetWorkState..'+isAgentMetWorkState+'...ismCloudIpNetWorkState..'+ismCloudIpNetWorkState+'..mRetData..'+mRetData);
           if(isCallbackState){
              if(isAgentMetWorkState){
                isCallbackState = false;
                mCallback(mAgentFwq,mRetData);
              }else {
                isCallbackState = false;
                if(ismCloudIpNetWorkState){
                  mCallback(mCloudIP,mRetData);
                }else {
                  mCallback('','');
                }
              }
           }
        }, 8000);
    },

    //通过网页拿代理和apMac
    getWebpage: function() {
        api.openFrame({
            name: 'altair',
            url: 'http://www.szaltair.com/',
            pageParam: {
                name: 'test'
            },
            rect: {
             x: 0,
             y: 0,
             w: '0',
             h: '0'
            },
        });
        //如监听网页的回调
        api.addEventListener({
            name: 'apiCloudSeeAgent'
        }, function(ret, err) {
            var appData = ret.value;
            if(api.systemType === 'ios'){
             appData = JSON.parse(appData);
            }
            var agent = appData.agent;
            var usermac = appData.usermac;
            var nasid = appData.nasid;
            seekAgent.getAgenIp(agent);
        });
    },
    //检查相关参数
    judgeWifiParam:function(wifiIp,apMac,phoneMac){
       if(wifiIp == null || wifiIp == undefined || wifiIp == ''){
         return false;
       }
       if(apMac == null || apMac == undefined || apMac == ''){
         return false;
       }
       if(phoneMac == null || phoneMac == undefined || phoneMac == ''){
         return false;
       }
       return true;
    },

    //获取项目需要的模式
    getSaveCombo:function(type,callback){
      if(type == 0){
        mSaveComboInt = 0;
      }
      mSaveComboInt++;
      var parame = {
          userid: $api.getStorage(NewFiData.appParameKey.userId),
          siteAreaid: $api.getStorage(NewFiData.appParameKey.siteAreaid),
      }
      if($api.getStorage(NewFiData.appParameKey.siteAreaid) > 0 && $api.getStorage(NewFiData.appParameKey.siteAreaid) < 10000){
          $api.setStorage(NewFiData.appParameKey.pattern,0)
          if(callback != null){callback(true);}
          return;
      }
      var parames = common.getJSONParame(NewFiData.keyword.payoffWay, JSON.stringify(parame));
      common.getConsole('模式请求','parame...'+JSON.stringify(parame)+'..fwq..'+$api.getStorage(NewFiData.appParameKey.appFWQ));
      common.ajax($api.getStorage(NewFiData.appParameKey.appFWQ),parames,8,function(ret, err) {
          if (ret) {
              common.getConsole('模式请求结果',JSON.stringify(ret));
              if(ret.code == 1){
                if(ret.pattern.length > 0){
                      if(ret.pattern[0] == 20){
                        if(ret.vip.result == 1){
                            seekAgent.getSaveCombo1(ret.pattern[0],ret.vip.vipnum,ret.vip.pledge,true,'',callback);
                        }else {
                            seekAgent.getSaveCombo2(ret.pattern[0],mSaveComboInt,callback);
                        }
                      }else if (ret.pattern[0] == 10 ||  ret.pattern[0] == 11 || ret.pattern[0] == 12) {
                        if(ret.combo.result == 1){
                            seekAgent.getSaveCombo1(ret.pattern[0],ret.combo.combonum,ret.combo.money,true,ret.combo.endTimg,callback);
                        }else {
                             seekAgent.getSaveCombo2(ret.pattern[0],mSaveComboInt,callback);
                        }
                      }else {
                           seekAgent.getSaveCombo2(ret.pattern[0],mSaveComboInt,callback);
                      }
                }else {
                     seekAgent.getSaveCombo2(0,mSaveComboInt,callback);
                }
              }else {
                 seekAgent.getSaveCombo2(0,mSaveComboInt,callback);
              }

          } else {
              common.getConsole('首页数据请求失败',JSON.stringify(err));
              seekAgent.getSaveCombo2(0,mSaveComboInt,callback);
          }
      })
    },

    //缓存一些商品信息和vip信息和模式
    //商品id、价格 / vip 状态、到期时间
    getSaveCombo1:function(pattern,commodity_id,commodity_pledge,vip_State,vip_expireTiem,callback){
      console.log("...pattern..."+pattern+'..commodity_id..'+commodity_id+'..commodity_pledge..'+commodity_pledge+'..vip_expireTiem..'+vip_expireTiem)
        if(pattern == undefined){pattern = 0};
        if(commodity_id == undefined){commodity_id = 0};
        if(commodity_pledge == undefined){commodity_pledge = 0};
        if(vip_State == undefined){vip_State = false};
        if(vip_expireTiem == undefined){vip_expireTiem = ''};
        $api.setStorage(NewFiData.appParameKey.pattern,pattern);
        $api.setStorage(NewFiData.appParameKey.commodity_id,commodity_id);
        $api.setStorage(NewFiData.appParameKey.commodity_pledge,commodity_pledge);
        $api.setStorage(NewFiData.appParameKey.vip_State,vip_State);
        $api.setStorage(NewFiData.appParameKey.vip_expireTiem,vip_expireTiem);
        if(callback != null){callback(true);}
    },
    //再次请求的处理  mSaveComboInt 次数
    getSaveCombo2:function(pattern,mSaveComboInt,callback){
        if(mSaveComboInt < 2){
          setTimeout(function(){
             seekAgent.getSaveCombo(1,callback);
          },800);
        }else {
            seekAgent.getSaveCombo1(pattern,0,0,false,'',callback);
        }
    }

};
